package cn.edu.zsc.blms.service.collection;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.Reader;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import cn.edu.zsc.blms.service.IReaderService;

public class ReaderService extends BaseService<Reader> implements IReaderService {
    private static IReaderService singleton;
    private ReaderService(){}
    public static IReaderService getSingleton(){
        if(singleton == null) singleton = new cn.edu.zsc.blms.service.collection.ReaderService();
        return singleton;
    }
    @Override
    public void create(){
        System.out.println("请输入姓名: ");
        String name = App.input.nextLine();
        System.out.println("请输入电话: ");
        String phone = App.input.nextLine();
        Reader reader = new Reader(name, phone);
        DATA.put(reader.id, reader);
        System.out.println("成功添加1位读者! ");
    }
    public void disable(){
        // 禁用读者
        System.out.println("请输入要禁用的读者ID: ");
        try{
            Reader reader = getEntity();
            reader.disable = true;
            System.out.println("读者" + reader.id + "已被禁用");
        }catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
    }
    @Override
    public void update(){
        // 修改读者
        System.out.println("请输入要修改的读者ID: ");
        try {
            Reader reader =  getEntity();
            System.out.println("请输入姓名, 直接按回车表示不修改: ");
            String name = App.input.nextLine();
            reader.name = "".equals(name) ? reader.name : name;
            System.out.println("请输入电话, 直接按回车表示不修改: ");
            String phone = App.input.nextLine();
            reader.phone = "".equals(phone) ? reader.phone : phone;
            System.out.println("用户" + reader.id + "的信息已被修改");
        }catch (EntityNotExistException e){
            System.out.println(e.getMessage());
        }
    }
    @Override
    public void query(){
        //查询读者
        System.out.println("请输入要查询的读者姓名: ");
        String name = App.input.nextLine();
        for(int i = 0;i < Reader.idCounter; ++i){
            Reader reader = DATA.get(i);
            if(reader.name != null && reader.name.contains(name)){
                System.out.println(reader);
            }
        }
    }
}
